// 
// index.js
// 海石后台管理
// 
// 2024/12/03.
//

import VeryMap from './VeryMap.vue';
import AMapLoader from "@amap/amap-jsapi-loader"

/**
 * 初始化通用地图插件
 * @param app
 */
const veryMap = (app) => {
    app.component('VeryMap', VeryMap);
    if ($config.map === "B_MAP") {
        installBMap()
    }
    if ($config.map === "A_MAP") {
        installAMap()
    }
}
/**
 * 初始化百度地图插件
 */
const installBMap = () => {
    window.$$bMapDidInstall = () => {
        console.primaryText("百度地图插件初始化成功")
        window.dispatchEvent(new CustomEvent('onamap'));
    }
    const script = document.createElement("script");
    script.src = `https://api.map.baidu.com/api?v=1.0&type=webgl&ak=${$config.bMapKey}&callback=$$bMapDidInstall`;
    document.body.appendChild(script);
}
/**
 * 初始化高德地图插件
 */
const installAMap = () => {
    window._AMapSecurityConfig = {
        key: $config.aMapkey,
        securityJsCode: $config.securityJsCode
    }
    AMapLoader.load({
        "key": $config.aMapkey,
        "version": "2.0",
        "plugins": ["AMap.DistrictSearch", "AMap.Scale", "AMap.ToolBar", "AMap.ControlBar", "AMap.AutoComplete", "AMap.Driving", "AMap.Geolocation", "AMap.ContextMenu", "AMap.MoveAnimation", "AMap.MarkerCluster"],
        AMapUI: {
            version: "1.1",
            plugins: ["misc/PositionPicker"]
        }
    }).then((AMap) => {
        console.primaryText("高德地图插件初始化成功")
        window.AMap = AMap
        window.dispatchEvent(new CustomEvent('onamap'));
    }).catch(e => {
        console.danger('高德地图出错', e);
    })
}

export default veryMap